<?
$conn = mysql_connect('localhost', 'zjxia', 'bblovebb');
mysql_select_db('crm');
$sql = 'select code, pos, digit from phone_recognition';
$result = mysql_query($sql);
$patterns = array();
while (list($code, $pos, $digit) = mysql_fetch_array($result)) {
	$patterns["$code\t$pos"] = $digit;
}
if (isset($_POST['file_name']) && isset($_POST['phone_num'])) {
	$file_name = $_POST['file_name'];
	$phone_num = $_POST['phone_num'];
	if (strlen($phone_num) != 11) {
		$err_str = 'length of phone num is not 11';
	}
	else {
		for ($pos = 0; $pos < 11; ++$pos) {
			$code = substr($file_name, $pos * 4, 4);
			$digit = substr($phone_num, $pos, 1);
			if (!isset($patterns["$code\t$pos"])) {
				$sql = "insert into phone_recognition(code, pos, digit, file_name, phone_num) values('$code', $pos, $digit, '$file_name', '$phone_num')";
				mysql_query($sql);
				$patterns["$code\t$pos"] = $digit;
#				echo "$sql\n";
			}
			else if ($patterns["$code\t$pos"] != $digit) {
				$err_str = "recognition conflict $code $pos";
			}
			else {
//				echo 'omit';
			}
		}
	}
}
if (isset($_POST['phone_num_delete'])) {
	$sql = "delete from phone_recognition where phone_num = ".$_POST['phone_num_delete'];
	mysql_query($sql);
}

$sql = 'select domain, city, query, title, url, phone_image_name, phone_num, contact_person, phone_digest, crawl_number from snapshot_ganji';
$result = mysql_query($sql);
while (list($domain, $city, $query, $title, $url, $phone_image_name, $phone_num, $contact_person, $phone_digest, $crawl_number) = mysql_fetch_array($result)) {
	if ($phone_num != '' || strlen($phone_image_name) != 48) continue;
	$file_name = substr($phone_image_name, 0, 44);
	$guess = '';
	for ($pos = 0; $pos < 11; ++$pos) {
		$code = substr($phone_image_name, $pos * 4, 4);
		if (isset($patterns["$code\t$pos"])) {
			$guess .= $patterns["$code\t$pos"];
		}
		else {
			$guess .= '?';
		}
	}
	if (strpos($guess, '?') === false) {
		$sql = "insert into snapshot(domain, city, query, title, url, phone_image_name, phone_num, contact_person, phone_digest, crawl_number) values('$domain', '$city', '$query', ".addslashes($title).", '$url', '$phone_imge_name', '$guess', ".addslashes($contact_person).", '', $crawl_number)";
		mysql_query($sql);
		$sql = "delete from snapshot_ganji where phone_image_name = '$phone_image_name'";
		mysql_query($sql);
	}
	else {
		$sql = 'select count(*) from snapshot_ganji';
		$result = mysql_query($sql);
		list($count) = mysql_fetch_array($result);
		break;
	}
}
/*
$fp = fopen('gj_phone_img.txt', 'r');
$guess = '';
$file_name = '';
$count = 0;
while (!feof($fp)) {
	$line = fgets($fp);
	$line = chop($line);
	$arr = explode("\t", $line);
	if (count($arr) != 12) continue;
	$guess = '';
	for ($pos = 0; $pos < 11; ++$pos) {
		$code = $arr[$pos + 1];
		if (isset($patterns["$code\t$pos"])) {
			$guess .= $patterns["$code\t$pos"];
		}
		else {
//			$err_str .= "($code, $pos) ";
			$guess .= '?';
		}
	}
	if (strpos($guess, '?') === false) {
		++$count;
	}
	else {
		$file_name = $arr[0];
		break;
	}
}
fclose($fp);
if ($count > 5000) {
	$err_str = '都识别完了';
}
*/
?>
<html><head><meta http-equiv="content-type" content="text/html; charset=UTF-8" />
</head><body>
<p><font color="red"><?echo $err_str?></font>还剩<?echo $count?>个号码未识别</p>
<form method="POST"><input type="hidden" name="file_name" value="<?echo $file_name?>"><img src="gj_phone_img/<?echo $file_name?>.png"> &nbsp; 猜测：<?echo $guess?> &nbsp; <input name="phone_num"><input type="submit"></form>
<?
echo '<p>';
$sql = 'select distinct(file_name), phone_num from phone_recognition';
$result = mysql_query($sql);
while (list($file_name, $phone_num) = mysql_fetch_array($result)) {
	echo "<form method='POST'><img src='gj_phone_img/$file_name.png'> $phone_num<input type='hidden' name='phone_num_delete' value='$phone_num'><input type='submit' value='删除'></form>";
}
echo '</>';
mysql_close();
?>
</body></html>

